require_dependency "search"
class Gallery < ActiveRecord::Base
  belongs_to                :photo
  belongs_to                :gallery_template
  belongs_to                :price_list
  has_many                  :partnerships
  has_many                  :photos,    :through => :partnerships, :order => "created_at asc"
  has_many                  :videos,    :through => :partnerships, :order => "created_at asc"
  has_many                  :users,     :through => :partnerships, :order => "login"
  has_many                  :pages,     :through => :partnerships, :order => "title"
  has_many                  :approvals
  has_many                  :items
  validates_presence_of     :name
  validates_presence_of     :description
  validates_presence_of     :gallery_template
  searches_on               :name,
                            :description
  
  has_friendly_id           :name,      :use_slug => true
  
  before_save               :update_approval_items
  after_save                :create_partnerships
  after_destroy             :destroy_dependents
  
  def private?
    !self.users.empty?
  end
  
  def sanitized_description
    self.text_description ? (self.text_description.gsub(/\\|'/) { |c| "\\#{c}" }.gsub("\r","").gsub("\n","")) : ""
  end
  
  def text_description
    self.description.gsub(/<\/?[^>]*>/,  "")
  end
  
  def purchasable?
    self.price_list ? true : false
  end
  
  def is_private?
    !self.users.empty?
  end
  
  def public?
    self.users.empty?
  end
  
  def comments
    self.all_items.empty? ? [] : ((comments = self.all_items.collect {|item| item.comments unless item.comments.empty?}.reject {|item| item.nil?}.flatten) ? comments : [])
  end
  
  def thumbnail
    (self.photo == nil) ? '/images/assets/default_thumb.jpg' : self.photo.public_filename(:small)
  end
  
  def visible_to?(current_user, logged_in)
    logged_in ? ((self.users.include?(current_user) or self.public? or current_user.is_admin?) ? true : false) : (self.public? ? true : false)
  end
  
  def all_items=(all_items = [])
    @gallery_items = all_items
  end
  
  def all_items
    @gallery_items = [] if @gallery_items.nil?
    ["photos", "videos"].each {|method| self.send(method).each {|gallery_item| @gallery_items << gallery_item if self.send(method)} if Gallery.method_defined?(method)} if @gallery_items.empty?
    @gallery_items
  end
  
  def items_needing_review
    self.all_items.reject {|item| item unless (item.approvals.empty? or self.approvals.reject {|approval| (approval.photo ? approval.photo : approval.video) != item}.empty?)}
  end
  
  def photo_ids=(photo_ids = [])
    @photo_ids = photo_ids
  end
  
  def video_ids=(video_ids = [])
    @video_ids = video_ids
  end
  
  def user_ids=(user_ids = [])
    @user_ids = user_ids
  end
  
  def page_ids=(page_ids = [])
    @page_ids = page_ids
  end
  
  def approval_items=(approval_items = [])
    @approval_items = approval_items
  end

  def create_approved_gallery
    if self.is_approved?
      gallery = Gallery.create!(:name => self.name + " - Approved", :private => true, :description => "Here is a list of your approved items.", :photo => (self.photo ? self.photo : nil))
      ["photos", "videos", "users"].each {|method| self.send(method).each {|gallery_item| gallery.send(method) << gallery_item if self.send(method)} if Gallery.method_defined?(method)}
    end
  end

protected  
  def update_approval_items
    return if @approval_items.nil?
    @approval_items.each do |item|
      if item[1]["photo_id"]
        Photo.find(item[1]["photo_id"].first.to_i).approval_in(self).approve! if item[1]["approval"] == "approve"
        Photo.find(item[1]["photo_id"].first.to_i).approval_in(self).decline! if item[1]["approval"] == "decline"
      elsif item[1]["video_id"]
        Video.find(item[1]["video_id"].to_i).approval_in(self).approve! if item[1]["approval"] == "approve"
        Video.find(item[1]["video_id"].to_i).approval_in(self).decline! if item[1]["approval"] == "decline"
      end
    end
  end
  
  def create_partnerships
    self.partnerships.each {|partnership| partnership.destroy if partnership.user and @user_ids or partnership.page and @page_ids} if self.partnerships
    @user_ids.each {|user_id| Partnership.new(:gallery_id => self.id, :user_id => user_id).save!} unless @user_ids.nil?
    @page_ids.each {|page_id| Partnership.new(:gallery_id => self.id, :page_id => page_id).save!} unless @page_ids.nil?
  end
  
  def destroy_dependents
    self.partnerships.each {|partnership| partnership.destroy} unless self.partnerships.empty?
    self.approvals.each {|approval| approval.destroy} unless self.approvals.empty?
  end
end